package cn.gene.timeyan.article.webapi.service.impl;



import cn.gene.timeyan.article.service.ITopService1;
import cn.gene.timeyan.article.webapi.mapper.ArticleMapper;
import cn.gene.timeyan.article.webapi.mapper.TopMapper;
import cn.gene.timeyan.article.webapi.mapper.TopMapper2;
import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.article.model.Article;
import cn.gene.timeyan.pojo.article.model.Top;
import cn.gene.timeyan.pojo.ums.dto.VipUserIdDTO;
import cn.gene.timeyan.ums.service.IVIPService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 缃?《琛 服务实现类
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
@Slf4j
@DubboService
@Service
public class TopServiceImpl1 implements ITopService1 {

    @Autowired
    TopMapper2 topMapper2;

    @Autowired
    ArticleMapper articleMapper;

    @DubboReference
    IVIPService ivipServicel;

    /**
     *
     * 大哥怎么说就怎么做吧.jpg
     * @param articleId 原帖子类Id
     * @param startTime
     * @param endTime
     * @return
     */

    @Override
    public int addNewTopService(Long userId,Long articleId, Date startTime, Date endTime) {
        VipUserIdDTO vipUserIdDTO = ivipServicel.IGetVipByUserIdService(userId);
        if (vipUserIdDTO == null){
            throw new TimeyanServiceException(ResponseCode.BAD_REQUEST,
                    "权限不足（" + ResponseCode.BAD_REQUEST.getValue() + "），你需要氪金!!!!");
        }
        System.out.println("***************");
        Article article = articleMapper.getArticleByArticleId(articleId);
        if (article == null){
            System.out.println("找不到帖子");
            throw new TimeyanServiceException(ResponseCode.BAD_REQUEST,
                    "帖子不存在（" + ResponseCode.BAD_REQUEST.getValue() + "），稍后再尝试");

        }
        System.out.println("====================");
        System.out.println(article);
        Top top = new Top();
        BeanUtils.copyProperties(article, top);
        top.setStartTime(startTime);
        top.setEndTime(endTime);
        System.out.println(top);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        top.setCreateTime(date);
        top.setUpdateTime(date);
       int row = topMapper2.addTopMapper(top);


        if (row!= 1){
            throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,
                    "添加失败,服务器忙（" + ResponseCode.INTERNAL_SERVER_ERROR.getValue() + "），请稍后再次尝试！");

        }


        return row;
    }
}
